// 导出excel js
  import { getToken } from '@/utils/auth'
  import store from '@/store'
import axios from 'axios'
export default function export_excel(URLs , data , file_Name="Downloaded_file" ,downLodeType='.xlsx'){
  /*
      * @URLs: 请求地址路径
      * @data: 请求传参
      * @user: wqz
    * */

  const today = new Date();
  const year = today.getFullYear();
  const month = today.getMonth() + 1; // 注意月份从0开始，所以要加1
  const date = today.getDate();
return  new Promise(function(resolve, reject) {
  file_Name = file_Name+'_'+year+'-'+month+'-'+date

       var url = process.env.VUE_APP_BASE_API +'/'+URLs
       var  token = getToken()
       axios({
           url: url,
           method: 'post',
           data,
           responseType: 'blob',
           headers: {
             'Content-Type': 'application/json',
             'Token' : token,
             'Lang' : store.getters.language=="zh"?'zh_CN':'ko_KR'
           }// 重点在于配置responseType: 'blob'
       }).then(res => {
         var name = res.headers['content-disposition'];
                 let fileName = '';
                 if (name) {
                   const str1 = name.replace(' ', '');
                   const arr1 = str1.split(';');
                   arr1.some(item => {
                     if (item.indexOf('filename') != -1) {
                       fileName = file_Name+downLodeType
                     }
                     return item.indexOf('filename') != -1;
                   });
                   if (fileName === ''){
                     fileName = file_Name+downLodeType
                   }
                 } else {
                   fileName = file_Name+downLodeType
                 }
           var link = document.createElement('a');  // 创建元素
           let blob = new Blob([res.data]);
           link.style.display = 'none';
           link.href = URL.createObjectURL(blob);   // 创建下载的链接
           link.setAttribute('download', decodeURI(decodeURI(fileName)));  // 给下载后的文件命名
           document.body.appendChild(link);
           link.click();  // 点击下载
           document.body.removeChild(link);  //  下载完成移除元素
           window.URL.revokeObjectURL(link.href);  // 释放掉blob对象
           resolve()
       }).catch(err => {
         console.log(err)
         reject()
         this.$message({
           type:'error',
           message:'Download failed'
         })
       });
})


}
